package com.hq.fxh.No5_User;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

import com.google.gson.Gson;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.CityModle;
import com.hq.fxh.No6UerSpace.Fragment.Fragment_UserSpace_AnLi;
import com.hq.fxh.No6UerSpace.Fragment.Fragment_city2Select;
import com.hq.fxh.No6UerSpace.Fragment.Fragment_citySelect;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CitySelectActivity extends AppCompatActivity {

    protected FrameLayout fragmentCitySelect;

    private Fragment_city2Select cityfragment;
    FragmentManager supportFragmentManager;
    List<CityModle.DataBean> prodata;
    private Fragment_citySelect provincefragment;
    int cityID, proID;
    String cityName, proName;
    CityModle.DataBean.CitysBean cityData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_city_select);
        initView();
        initData();
        getCityData();

    }

    private void initData() {
        provincefragment = Fragment_citySelect.newInstance(0);

        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

        cityfragment.setCityCallBack(new Fragment_city2Select.CityCallBack() {
            @Override
            public void selectCall(int position, Object data) {
                cityData = (CityModle.DataBean.CitysBean) data;
                cityID = cityData.getArea_id();
                cityName = cityData.getArea_name();
                Intent data1 = new Intent();
                data1.putExtra("cityName", cityName);
                data1.putExtra("proName", proName);
                data1.putExtra("cityID", cityID);
                data1.putExtra("proID", proID);
                CitySelectActivity.this.setResult(11, data1);
                finish();

            }
        });
        provincefragment.setCityCallBack(new Fragment_citySelect.CityCallBack() {
            @Override
            public void selectCall(int position, Object data) {
                if (prodata != null && prodata.size() > position) {
                    CityModle.DataBean dataBean = prodata.get(position);
                    proID = dataBean.getArea_id();
                    proName = dataBean.getArea_name();
                    List<CityModle.DataBean.CitysBean> citys = dataBean.getCitys();

                    cityfragment.setCityData(citys, 1);



                    supportFragmentManager.beginTransaction().replace(R.id.fragment_citySelect, cityfragment).commit();
                } else {
                    LogUtils.startToash(CitySelectActivity.this, "城市选择出错");
                }

            }
        });


        fragmentTransaction.replace(R.id.fragment_citySelect, provincefragment);

        //    fragmentTransaction.add(R.id.fragment_citySelect, cityfragment);

        fragmentTransaction.commit();

    }

    private void initView() {
        fragmentCitySelect = (FrameLayout) findViewById(R.id.fragment_citySelect);
    }


    public void getCityData() {
        UserServiceImpl.getCity(new Callback<ResponseBody>() {

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body != null) {
                    try {
                        String string = body.string();
                        CityModle cityModle = new Gson().fromJson(string, CityModle.class);
                        prodata = cityModle.getData();
                        provincefragment.setProData(prodata, 0);
                        LogUtils.e(string);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    ResponseBody responseBody = response.errorBody();
                    if (response.body() != null) {
                        try {
                            LogUtils.e(responseBody.string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("error  " + t.getMessage());
            }
        });
    }
}
